package com.joyance.test_servlet.servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.joyance.test_servlet.common.ClassEnum;

public class ForwardServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println("ForwardServlet get username [value=" + username + "]");
        String testClass = req.getParameter("testClass");
        ClassEnum classEnum = ClassEnum.valueOf(Integer.parseInt(testClass));
        if (classEnum == ClassEnum.ServletContext) {
            req.setAttribute("username", username);
            super.getServletContext().setAttribute("username", username);
            super.getServletContext().setAttribute("password", "123456");
        }
        if (classEnum == ClassEnum.HttpServletRequest) {
            resp.setHeader("Content-disposition", "attachment;");
            resp.addHeader("sex", "female");
            Cookie cookie = new Cookie("age", "27");
            resp.addCookie(cookie);
            req.getSession().setAttribute("animal", "bear");
            resp.sendRedirect(req.getContextPath() + "/test/" + classEnum.getName() + "/asdf.do?id=5&age=27");
            return;
        }
        if (classEnum == ClassEnum.HttpServletResponse) {
            Cookie cookie = new Cookie("age", "27");
            resp.addCookie(cookie);
            resp.addHeader("age", "27");
            resp.setHeader("name", "joyance");
            String originalURL = req.getContextPath() + "/test/" + classEnum.getName() + "?name=关悦&age=27";
            String encodeUrl = resp.encodeRedirectURL(originalURL);
            System.out.println("encodeRedirectURL=" + encodeUrl);
            resp.sendRedirect(URLEncoder.encode(originalURL, "UTF-8"));
            return;
        }
        if (classEnum == ClassEnum.HttpSession) {
            HttpSession httpSession = req.getSession();
            httpSession.setAttribute("name", "joyance");
            httpSession.setAttribute("age", 27);
        }
        if (classEnum == ClassEnum.Cookie) {
            Cookie cookie = new Cookie("flush", "1000");
            // cookie.setDomain(".foo.com");
            // setMaxAge在浏览器才看看出效果
            // cookie.setMaxAge(3600);
            cookie.setSecure(true);
            cookie.setVersion(10);
            cookie.setValue("newValue");
            // setPath与cookie.setDomain类似
            cookie.setPath(req.getContextPath() + "/test/Cookie");
            resp.addCookie(cookie);
            resp.sendRedirect(req.getContextPath() + "/test/" + classEnum.getName());
            return;
        }
        req.getRequestDispatcher("/test/" + classEnum.getName()).forward(req, resp);
    }


    // 获得指定文件的byte数组
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

}
